<template>
  <div class="partNewsIndex">
    <Aside></Aside>
    <Header :title="title" @toggleMenu="toggleMenu" @navToBack="navToBack">
    </Header>
    <div class="heart">
      <div class="heartBanner">
        <img src="http://dangjian.co-links.com/Static/Home/images/heartEditor.jpg" alt="">
      </div>
      <div class="heartForm">
        <form action="" method="post" return="false">
          <div class="formItem">
            <label  >
              入党时间：</label>
            <div class=" heartInput" @click.stop="calInit">
                {{choiseDate}}
            </div>
          </div>
          <div class="formItem">
            <label  >
              所属部门：</label>
            <div class="heartInput heartInput2" >
              <input type="text" v-model="choiseDeparment" placeHolder="请选择所属部门">
            </div>
          </div>
          <div class="formItem">
            <label for="content">向党说句心里话：</label>
            <div class="heartTextArea">
              <textarea name="content" id="content" ref="content" placeholder="不超过20字"></textarea>
            </div>
          </div>
          <div class="formItem" style="margin-top: 0.4rem;height: 2rem">
            <label for="content">我的自拍：</label>
            <div class=" mySelfPhoto">
              <span class="inputFile" @click="addPic"></span>
              <input type="file"  ref="fileInput" accept="image/jpeg,image/jpg,image/png,image/gif"  @change="emitAddPic">
              <div>
                上传照片
              </div>
            </div>
            <div class=" weui-cells_form ">
              <div class="weui-uploader">
                <div class="weui-uploader__bd">
                  <ul class="weui-uploader__files" id="uploaderFiles" ref="uploaderFiles">
                    <li class="weui-uploader__file" v-for="(item,index) in picArray" :style="{backgroundImage:'url('+item.pic+')'}">
                      <span class="deletIcon" @click.stop="deleteIcon(index)"></span></li>
                  </ul>
                </div>
              </div>
            </div>
          </div>
          <div class="sendBtn">
            <span @click.stop="contribute">提交投稿</span>
          </div>
        </form>
      </div>
        <div class="tips" v-if="tips">
          <span>{{tipsHtml}}</span>
        </div>
      <Alarm :alarm="alarm" v-if="showAlarm" @cancel="cancel" @confirm="confirm"></Alarm>
    </div>
  </div>
</template>
<script >
  import * as qiniu from 'qiniu-js'
  import aside from '../../components/aside.vue'
  import header from '../../components/header.vue'
  import weui from '../../../static/js/weui.js'
  import loading from '../../components/loading.vue'
  import Alarm from '../../components/alarm.vue'
  import {mapState} from 'vuex'
  import {getQueryParam} from '@/utils/index.js'
  export default({
    data (){
      return {
        nomoreData:false,
        loadmoreClick:false,
        title:'投稿',
        showLoading:false,
        choiseDate:'请选择入党时间',
        picArray:[],
        tempImgArray:[],
        pathStr:'',
        tipsHtml:'',
        tips:false,
        str:'',
        alarm:'',
        showAlarm:false,
        progress:'',
        choiseDeparment:''
      }
    },
    components:{
      Aside:aside,
      Header:header,
      Loading:loading,
      Alarm:Alarm
    },
    methods:{
      emitAddPic(){
        this.addPic()
      },
      cancel (){
        this.showAlarm =false
      },
      confirm (){
        this.showAlarm =false
      },
      addPic(){
        var files =this.$refs.fileInput.files
//        console.log(files[0])
        let that =this
        var url = window.URL || window.webkitURL || window.mozURL ;
        if(files[0]){
          that.showLoading =true
          var observable = qiniu.upload(files[0], null, this.token,null)
          var observer = {
            next(res){
//              console.log(res)
              this.progress =res.total.percent
//              alert(this.progress)
//              alert(123)
            },
            error(err){
              console.log(err)
              alert(err)
              that.showLoading =false
            },
            complete(res){
//                alert(res)
              var str ='http://oss.co-links.com/'+ res.hash+'#'
//              alert(str)
              that.pathStr =str
              that.showLoading =false
//              console.log(str)
//              console.log(that.tempImgArray)
//              that.tempImgArray[that.tempImgArray.length] =str
            }
          }
          var subscription = observable.subscribe(observer) // 上传开始
          if(files[0].size/1024>5120){
            this.showAlarm =true
            this.alarm='上传文件不得超过5M'
            return false
          }
        }
//        this.picArray.pic =url.createObjectURL(files[0])
//        console.log(this.picArray.length)
        if(this.picArray.length>=1){
            return false
        }else{
          for(var i=0;i<files.length;i++){
           this.picArray.push({
                  pic:url.createObjectURL(files[0])
                })
          }
        }
      },
      calInit(){
        let that =this
        weui.datePicker({
          start: 1989,
          end: new Date().getFullYear(),
          defaultValue: [new Date().getFullYear(), new Date().getMonth() * 1 + 1],
          endMonth: new Date().getMonth() * 1,
          onChange: function (result) {
//            console.log(result)
          },
          onConfirm: function (result) {
            that.rankListItem =[]
            that.defaultPage =1
            var year = result[0];
            var month = result[1];
            var time = year + '-' + month;
            that.str =time
//            that.initData(that.str)
            that.choiseDate =that.str
          }
        })
      },
      navToBack(){
        this.$router.go(-1);//返回上一层
      },
      toggleMenu(){
        $('.aside').toggleClass('active')
        $('body').toggleClass('active')
        if($('.aside').hasClass('active')){
          $('#toggleBtn').find('.img2').hide()
          $('#toggleBtn').find('.img1').show()
        }else{
          $('#toggleBtn').find('.img1').hide()
          $('#toggleBtn').find('.img2').show()
        }
      },
      loadMore (){
        this.loadmoreClick =!this.loadmoreClick
      },
      choiseHead(index){
        this.defaultIndex =index
      },
      contribute(){
          let that =this
          this.showLoading=true
          if(!that.str){
            that.showAlarm=true
            that.alarm ='请选择入党时间'
            return false
          }
//          alert(that.pathStr)
          if(!that.pathStr){
            that.showAlarm=true
            that.alarm ='请选择自拍照片'
            return false
          }
        if(this.$refs.content.value.length==0){
          that.showAlarm=true
          that.alarm ='请填写向党说的心里话'
          return false
        }else if(this.$refs.content.value.length>20){
          that.showAlarm=true
          that.alarm ='内容不得超过20字'
          return false
        }
          this.axios.post('/home/Heart/contribute?'+`${this.baseUrl}`,{
            rudang_time:this.str,
            content:this.$refs.content.value,
            uid:this.uid,
            take:this.pathStr,
            department:this.choiseDeparment,
            company_id:this.company_id
          })
          .then((res)=>{
            if(res.data.code==1){
              this.tips =true
              this.$refs.content.value=''
              that.tipsHtml =res.data.msg
              that.choiseDate='请选择入党时间'
              that.choiseDeparment=''
              that.pathStr =''
              that.str =''
              that.picArray =[]
              setTimeout(()=>{
                that.tips =false
              },1500)
            }
          })
      },
      deleteIcon(index){
        this.picArray.splice(index,1)
      },
    },
    mounted (){
      $('body').removeClass('active')
      this.axios.get('/home/Qiniu/upload?'+`${this.baseUrl}`)
        .then((res)=>{
          this.token =res.data.token
        })
    },
    computed:{
      ...mapState({
        uid:state=>state.app.loginState.uid||localStorage.getItem('uid'),
        company_id:state=>state.app.company_id||getQueryParam('identifier',window.location.href).slice(0,getQueryParam('identifier',window.location.href).indexOf('/'))
      })
    }
  })
</script>
<style scoped="">
  .deletIcon{
    position:absolute;
    width: 20px;
    height: 20px;
    background: #f43530;
    right: 0;
    top: -10px;
    border-radius: 20px;
    z-index: 5;
  }
  .deletIcon::before {
    content: '';
    width: 10px;
    height: 4px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #fff;
  }
  .inputFile{
    display: inline-block;
    width:0.98rem;
    height: 0.98rem;
    background-image: url("../../../static/images/inputFile.jpg");
    background-repeat: no-repeat;
    background-position: center;
    position: relative;
    background-size: 0.98rem 0.98rem;
    z-index: 0;
  }
  .mySelfPhoto  input{
    position: absolute;
    left: 0;
    top:0;
    width: 100%;
    height: 100%;
    opacity: 0;
    z-index: 1;
  }
  .heart{
    margin-top: 0.8rem;
  }
  .heartBanner{
    margin-top: 0.8rem;
    height: 3.6rem;
    position: relative;
  }
  .haertContent{
    background: #f3debc;
    padding:  0 0.32rem;
    padding-top: 0.16rem;
  }
  .haertContent img{
  }
  .heartItem{
    background-image: url("../../../static/images/haertBg2.png");
    background-size: 6.88rem 18.09rem;
    background-repeat: no-repeat;
    height: 18.09rem;
    overflow: hidden;
  }
  .heartInner{
    padding: 0 0.25rem;
    margin-top: 0.78rem;
  }
  .heartList{
    height: 2.6rem;
    box-sizing: border-box;
    -webkit-box-sizing: border-box;
    border: 0.01rem dashed #dab66a;
    background: #f5eab4;
    vertical-align: top;
    padding: 0.2rem;
    display: -webkit-box;
    display: -webkit-flex;
    display: flex;
    margin-top: 0.2rem;
  }
  .heartList img{
    width: 2.44rem;
    height: 2.19rem;
    border: 0.01rem dashed #dab66a;
    margin-right: 0.18rem;
    box-sizing: border-box;
    -webkit-box-sizing: border-box;
  }
  .heartList .heartRight{
    -webkit-box-flex: 1;
    -webkit-flex: 1;
    flex: 1;
    min-width: 0;
    color: #a06801;
  }
  .heartRight .name{
    font-size: 0.32rem;
  }
  .heartRight .joinTime{
    font-size: 0.24rem;
    /* padding-bottom: 0.14rem; */
    /* padding-top: 0.07rem; */
  }
  .position{
    font-size: 0.26rem;
    line-height: 1.5;
  }
  .motto{
    color: #e83329;
    font-weight: bold;
    font-size: 0.3rem;
  }
  .heartBtn ,.sendBtn{
    text-align: center;
    padding-bottom: 0.79rem;
  }
  .heartBtn span,.sendBtn span{
    display: inline-block;
    font-size: 0.32rem;
    color: white;
    width: 4.5rem;
    height: 0.7rem;
    text-align: center;
    line-height: 0.7rem;
    background: #e83329;
    border-radius: 0.32rem;
    -webkit-border-radius: 0.32rem;
    margin-top: 0.7rem;
  }
  .heartForm{
    padding: 0.47rem 0.32rem 0.76rem 0.32rem;
    width: 100%;
    box-sizing: border-box;
    -webkit-box-sizing: border-box;
  }
  .formItem{
    display: -webkit-box;
    display: -webkit-flex;
    display: flex;
    position: relative;
    /*-webkit-box-align: center;*/
    /*-webkit-align-items: center;*/
    /*align-items: center;*/
  }
  .formItem label{
    width: 1.68rem;
    height: 0.7rem;
    vertical-align: middle;
  }
  .formItem .heartSelect,.heartInput{
    -webkit-box-flex: 1;
    -webkit-flex: 1;
    flex: 1;
    min-width: 0;
    height: 0.7rem;
    padding-left: 0.83rem;
  }

  .heartTextArea{
    height: 2rem;
    -webkit-box-flex: 1;
    -webkit-flex: 1;
    flex: 1;
    min-width: 0;
    -webkit-appearance: none;
    apperance:none;
    cursor: pointer;
    border-radius: 0.08rem;
    border: 1px solid #e6e6e6;
    margin-left: 0.3rem;
  }
  .mySelfPhoto{
    position: absolute;
    height: 2rem;
    left: 2rem;
  }
  .weui-cells_form{
    position: absolute;
    left: 2rem;
    top:0;
    width: 2rem;
    height: 2rem;
  }
  .mySelfPhoto input{
    position: absolute;
    left: 0;
    top:0;
    width: 0.98rem;
    height:0.98rem;
  }
  .heartSelect,.heartInput{
    border: 1px solid #e6e6e6;
    border-radius: 0.08rem;
    position: relative;
    margin-bottom: 0.2rem;

  }
  .heartInput{
    margin-left: 0.3rem;
    line-height: 0.7rem;
    text-align: right;
    padding-right: 50px;
    position: relative;
  }

  .heartInput:after{
    position: absolute;
    content: '';
    display: inline-block;
    border-width: 6px 6px 0 6px;
    border-color: #C8C8CD transparent transparent transparent;
    border-style: solid;
    position: relative;
    top: -2px;
    position: absolute;
    top: 50%;
    right: 15px;
    margin-top: -4px;
  }
  .heartInput2:after{
    border-width:0
  }
  .heartInput input{
    display: inline-block;
    width: 100%;
    height:0.66rem;
    -webkit-appearance: none;
    border: none;
    outline: none;
    text-align: right;
    background: transparent;

  }
  .heartSelect:after{
    content: " ";
    display: inline-block;
    border-width: 6px 6px 0 6px;
    border-color:#C8C8CD transparent transparent transparent ;
    border-style: solid;
    position: relative;
    top: -2px;
    position: absolute;
    top: 50%;
    right: 15px;
    margin-top: -4px;
  }
  .formItem .heartSelect select{
    -webkit-appearance: none;
    border: 0;
    outline: 0;
    background-color: transparent;
    width: 100%;
    font-size: inherit;
    height: 100%;
    line-height: 0.7rem;
    position: relative;
    z-index: 1;
    text-align: right;
    color:#C8C8CD ;
  }
  /*select {*/
  /*direction: rtl;*/
  /*}*/
  /*select option {*/
  /*direction: ltr;*/
  /*}*/
  .heartTextArea textarea{
    display: inline-block;
    width: 100%;
    height: 100%;
    -webkit-appearance: none;
    border: 0;
    outline: 0;
    background-color: transparent;
    padding: 10px 20px;
    box-sizing: border-box;
    -webkit-box-sizing: border-box;
    resize: none;

  }
  .weui-uploader__files li{
    width: 0.98rem;
    height: 0.98rem;
    position: relative;
  }
  .weui-uploader__bd{
    overflow: visible;
  }
</style>
